package com.huangwenshuo.futurestarschoolmanagement.service.impl;

import com.huangwenshuo.futurestarschoolmanagement.mapper.ReplyMapper;
import com.huangwenshuo.futurestarschoolmanagement.mapper.UsersMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.dto.CommentDto;
import com.huangwenshuo.futurestarschoolmanagement.pojo.dto.ReplyDto;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.Comment;
import com.huangwenshuo.futurestarschoolmanagement.mapper.CommentMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.Reply;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.CommentVo;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.ReplyVo;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.UserInfoVo;
import com.huangwenshuo.futurestarschoolmanagement.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangwenshuo
 * @since 2021-03-20
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private ReplyMapper replyMapper;
    @Override
    public List<CommentVo> getComByAid(Integer aid) {
        return commentMapper.getComByAid(aid);
    }

    @Override
    public void discuss(CommentDto commentDto) {
        UserInfoVo user = usersMapper.getUserInfo(commentDto.getUid());
        UserInfoVo toUser = usersMapper.getUserInfo(commentDto.getToId());
        if(toUser == null){
            Comment comment = new Comment();
            comment.setFromId(user.getUid());
            comment.setFromName(user.getUserName());
            comment.setFromAvatar(user.getAvatar());
            comment.setContent(commentDto.getContent());
            comment.setOwnerId(commentDto.getAid());
            commentMapper.addComment(comment);
        }else {
            Reply reply = new Reply();
            reply.setCommentId(commentDto.getCid());
            reply.setContent(commentDto.getContent());
            reply.setFromAvatar(user.getAvatar());
            reply.setFromId(user.getUid());
            reply.setFromName(user.getUserName());
            reply.setToId(toUser.getUid());
            reply.setToAvatar(toUser.getAvatar());
            reply.setToName(toUser.getUserName());
            replyMapper.addReply(reply);
        }
    }

    @Override
    public Map<String, Object> getCommentList(Integer page, Integer rows) {
        Map<String,Object> res = new HashMap<>();
        int temo = page;
        page = (page-1)*rows;
        rows = temo * rows;
        List<CommentVo> commentVos = commentMapper.getComByPage(page,rows);
        int total = commentMapper.getComTotal();
        res.put("commentVos",commentVos);
        res.put("total",total);
        return res;
    }

    @Override
    public Map<String, Object> getReplyList(ReplyDto replyDto) {
        Map<String,Object> res = new HashMap<>();
        int page = replyDto.getPage();
        replyDto.setPage((replyDto.getPage()-1)*replyDto.getRows());
        replyDto.setRows(page*replyDto.getRows());
        List<Reply> replyVos = replyMapper.getReplyByPage(replyDto);
        int total = replyMapper.getReplyTotal(replyDto);
        res.put("replyVos",replyVos);
        res.put("total",total);
        return res;
    }

    @Override
    public void del(Integer cid, Integer rid) {
        if(cid != null){
            commentMapper.delCid(cid);
        }else if (rid != null){
            replyMapper.delRid(rid);
        }
    }
}
